package cn.jhz.learn.blog.service.core.open.article.impl;

import cn.jhz.learn.blog.component.GLOBAL;
import cn.jhz.learn.blog.component.ValidationHolder;
import cn.jhz.learn.blog.dao.article.BlogArticleSubfieldMapper;
import cn.jhz.learn.blog.dao.file.BlogFileMapper;
import cn.jhz.learn.blog.po.article.BlogArticleSubfield;
import cn.jhz.learn.blog.po.blogger.BlogBlogger;
import cn.jhz.learn.blog.service.core.open.article.ArticleSubfieldService;
import cn.jhz.learn.blog.util.ObjUtil;
import cn.jhz.learn.blog.vo.SubfieldPageView;
import cn.jhz.learn.blog.vo.article.query.view.SubfieldView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("articleSubfieldService")
public class ArticleSubfieldServiceImpl implements ArticleSubfieldService {

    private final BlogArticleSubfieldMapper subfieldMapper;
    private final BlogFileMapper fileMapper;

    @Autowired
    public ArticleSubfieldServiceImpl(BlogArticleSubfieldMapper subfieldMapper, BlogFileMapper fileMapper) {
        this.subfieldMapper = subfieldMapper;
        this.fileMapper = fileMapper;
    }

    @Override
    public PageInfo<SubfieldView> getPage(@NotNull SubfieldPageView pageView) {

        List<Byte> visibility, status;
        List<SubfieldView> subfields;
        Integer  blogId;

        blogId = ((BlogBlogger)ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_BLOGGER.BLOG_NAME + pageView.getBlog_name().get())).getId();
        visibility = ValidationHolder.getLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.SUBFIELDS.VISIBILITY + pageView.getBlog_name().get());
        status = ValidationHolder.getLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.SUBFIELDS.STATUS + pageView.getBlog_name().get());

        PageHelper.startPage(pageView.getPage_num(), pageView.getPage_size());
        subfields = subfieldMapper.selectByBlogId(blogId, visibility, status);

        return PageInfo.of(subfields);
    }

    /**
     * 为前端提供status==1的数据
     * @param uid
     * @param pageView
     * @return
     */
    @Override
    public PageInfo<SubfieldView> getPage(Integer uid, @NotNull SubfieldPageView pageView) {
        Integer blogId;
        List<Byte> visibility,status;
        List<SubfieldView> subfields;

        if(pageView.getBlog_name().isPresent()){
            blogId = ((BlogBlogger)ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_BLOGGER.BLOG_NAME + pageView.getBlog_name().get())).getId();
            visibility = ValidationHolder.getLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.SUBFIELDS.VISIBILITY + pageView.getBlog_name().get());
            status = ValidationHolder.getLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.SUBFIELDS.STATUS + pageView.getBlog_name().get());
        }else{
            blogId = ValidationHolder.getCurrentUser().getId();
            visibility = ValidationHolder.getLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.SUBFIELDS.VISIBILITY + blogId);
            status = ValidationHolder.getLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.SUBFIELDS.STATUS + blogId);
        }

        PageHelper.startPage(pageView.getPage_num(), pageView.getPage_size());
        subfields = subfieldMapper.selectByBlogId(blogId, visibility, status);

        return PageInfo.of(subfields);
    }

    @Override
    public Map<String, Object> getDetail(Integer subfieldId) {
        return getStringObjectMap(subfieldId);
    }

    @Override
    public Map<String, Object> getDetail(Integer uid, Integer subfieldId) {
        return getStringObjectMap(subfieldId);
    }

    @NotNull
    private Map<String, Object> getStringObjectMap(Integer subfieldId) {
        Map<String, Object> count = subfieldMapper.countAccessAndSubscribeByPrimaryKey(subfieldId,
                Lists.newArrayList(GLOBAL.RESOURCE.STATUS.VALID_ALL));
        Map<String, Object> subfield =
                ObjUtil.transBeanMap(ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_ARTICLE_SUBFIELD.ID + subfieldId));

        subfield.put("imgUrl", fileMapper.selectByPrimaryKey((Integer) subfield.get("fileId")).getSignatureCode());
        subfield.putAll(count);
        subfield.remove("fileId");

        return subfield;
    }
}
